package com.ruoyi.interfaced.controller;

import java.sql.Array;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.html.parser.Entity;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.interfaced.domain.WechatUser;
import com.ruoyi.interfaced.service.impl.WeChatUserService;
import io.jsonwebtoken.Claims;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.interfaced.domain.ShopUserInter;
import com.ruoyi.interfaced.service.IShopUserInterService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户信息Controller
 * 
 * @author ruoyi
 * @date 2021-11-28
 */
@RestController
@RequestMapping("/interfaced/user")
public class ShopUserInterController extends BaseController
{
    @Autowired
    private IShopUserInterService shopUserService;

    /**
     * 支付扣除余额
     */
    @GetMapping("/deduction")
    public List deduction(ShopUserInter shopUserInter){
        List list = new ArrayList();
        int a = shopUserService.ShopUserDeduction(shopUserInter);
        if (a == 1){
            list.add("成功");
        }else{
            list.add("失败");
        }
        return list;
    }

    /**
     * 获取openid
     */
    @GetMapping("/openid")
    public String openId(String code){
        String appid = "wx149eddbf8ac111b6";
        String secret = "6b9dfb6cb25b6eb03262bab4e9d478bd";
        String openid = "https://api.weixin.qq.com/sns/jscode2session?appid="+appid+
                "&secret="+secret+"&js_code="+code+
                "&grant_type=authorization_code";
        String result = HttpUtils.sendGet(openid);
        // JsonParser类
        JsonParser jp = new JsonParser();
        //将json字符串转化成json对象
        JsonObject jo = jp.parse(result.toString()).getAsJsonObject();
        //获取errcode对应的值
        String errcode = jo.get("openid").getAsString();
        // openid 实体类
        WechatUser wechatUser = new WechatUser();
        wechatUser.setOpenid(errcode);
        // 加密 openid 工具类
        WeChatUserService service = new WeChatUserService();
        String a = service.token(wechatUser);
        return a;
    }

    /**
     * 添加用户
     */
    @GetMapping("add")
    public List<ShopUserInter> insert(ShopUserInter shopUserInter){
        WeChatUserService weChatUserService = new WeChatUserService();
        Claims a = weChatUserService.parseToken(shopUserInter.getWxOpenid());
        String WxOpenid = (String) a.get("openid");
        shopUserInter.setWxOpenid(WxOpenid);
        List<ShopUserInter> list = shopUserService.SelectShopUserListWxOpenid(WxOpenid);
        if (list.isEmpty()){
            int i = shopUserService.insert(shopUserInter);
            if (i == 1){
                List<ShopUserInter> lists = shopUserService.SelectShopUserListWxOpenid(WxOpenid);
                return lists;
            }else{
                return list;
            }
        }else{
            return list;
        }
//        shopUserl.setWxOpenid(openid);
//        System.out.println("openid"+openid);
//        int i = shopUserService.insert(shopUserl);

    }

    /**
     * 查询用户信息列表
     */
//    @PreAuthorize("@ss.hasPermi('order:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(ShopUserInter shopUser)
    {
        startPage();
        List<ShopUserInter> list = shopUserService.selectShopUserList(shopUser);
        return getDataTable(list);
    }

    /**
     * 导出用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('order:user:export')")
    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShopUserInter shopUser)
    {
        List<ShopUserInter> list = shopUserService.selectShopUserList(shopUser);
        ExcelUtil<ShopUserInter> util = new ExcelUtil<ShopUserInter>(ShopUserInter.class);
        util.exportExcel(response, list, "用户信息数据");
    }

    /**
     * 获取用户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:user:query')")
    @GetMapping(value = "/{wxOpenid}")
    public AjaxResult getInfo(@PathVariable("wxOpenid") String wxOpenid)
    {
        return AjaxResult.success(shopUserService.selectShopUserByWxOpenid(wxOpenid));
    }

    /**
     * 新增用户信息
     */
    @PreAuthorize("@ss.hasPermi('order:user:add')")
    @Log(title = "用户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShopUserInter shopUser)
    {
        return toAjax(shopUserService.insertShopUser(shopUser));
    }

    /**
     * 修改用户信息
     */
    @PreAuthorize("@ss.hasPermi('order:user:edit')")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShopUserInter shopUser)
    {
        return toAjax(shopUserService.updateShopUser(shopUser));
    }

    /**
     * 删除用户信息
     */
    @PreAuthorize("@ss.hasPermi('order:user:remove')")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{wxOpenids}")
    public AjaxResult remove(@PathVariable String[] wxOpenids)
    {
        return toAjax(shopUserService.deleteShopUserByWxOpenids(wxOpenids));
    }
}
